 
 var victimDocId = 0;
 $(document).ready(function() {
    $("#tabs").tabs();
    $( "#dialogNewProject").dialog({ autoOpen: false, resizable:false, modal:true }); 
    $( "#dialogDeleteProject").dialog({ autoOpen: false, resizable:false, modal:true }); 
    $( "#dialogRenameProject").dialog({ autoOpen: false, resizable:false, modal:true }); 
	
    $( "#newProjectButton" ).click(function() { 		
            $( "#dialogNewProject" ).dialog('open'); return false; 
    }); 
  $("#documents").load("includes/php/documents.php", function(response, status, xhr) {
      if (status == "error") {
         var msg = "Sorry but there was an error: ";
          $("#documents").html(msg + xhr.status + " " + xhr.statusText);
      }
      $( ".deleteButtonClass").click(function() {
           // $('#deleteProject').val($('#deleteButton').attr("doc_id")); 
            //$('input[name="hidden_delete_id"]').val($(this).attr("doc_id")); 
            victimDocId = ($(this).attr("doc_id")); 
            $( "#dialogDeleteProject" ).dialog('open'); return false; 
      });
      $( ".renameButtonClass").click(function() {
           // $('#deleteProject').val($('#deleteButton').attr("doc_id")); 
            victimDocId = ($(this).attr("doc_id")); 
            $( "#dialogRenameProject" ).dialog('open'); return false; 
      });
      jQuery("span.timeago").timeago($("span.timeago").attr("title"));
      
});
    
    $("#dialogNewProject").dialog('option', 'buttons', {  
            "Create" : function() { 
                var doc_id = victimDocId;
                var doc_name = $('input[name="doc_name"]').val(); 
                $.ajax({ 
                    type:"POST", 
                    url:"includes/php/ajax.php", 
                    data:"action=create&doc_name="+doc_name+"&doc_id="+doc_id,
                    success: function(xhr){  
                       // $('div.deleteMessage').hide(function(){$('div.deleteSuccess').fadeIn();}); 
//                            if (xhr == 'Too many characters. We limit names to 255 characters.') 
                            if ((xhr == 'Document name too long, we limit to 255 characters.') || 
                                 (xhr == 'Please enter a project name.'))
                            { 
                                $("#createErrorDiv").html(xhr); 
                            } 
                            else 
                            {
                                $("#createErrorDiv").html("");
                                $('input[name="doc_name"]').val(""); 
			        $("#dialogNewProject").dialog("close"); 
			        $("#documents").load("includes/php/documents.php", function(response, status, xhr) {
				    if (status == "error") {
					var msg = "Sorry but there was an error: ";
					$("#documents").html(msg + xhr.status + " " + xhr.statusText);
				    }
				    else {
					$( ".deleteButtonClass").click(function() {
						// $('#deleteProject').val($('#deleteButton').attr("doc_id")); 
						//$('input[name="hidden_delete_id"]').val($(this).attr("doc_id")); 
						victimDocId = ($(this).attr("doc_id")); 
						$( "#dialogDeleteProject" ).dialog('open'); return false; 
						});
					$( ".renameButtonClass").click(function() {
						// $('#deleteProject').val($('#deleteButton').attr("doc_id")); 
						victimDocId = ($(this).attr("doc_id")); 
						$( "#dialogRenameProject" ).dialog('open'); return false; 
						});
                                        jQuery("span.timeago").timeago($("span.timeago").attr("title"));
				    }
		    	       });
                            }
                        } 
                 });
            }, 
            "Cancel" : function() { 
                $(this).dialog("close"); 
            }
    }); 
    $("#dialogRenameProject").dialog('option', 'buttons', {  
            "Rename" : function() { 
                var doc_id = victimDocId;
                var new_doc_name = $('input[name="new_doc_name"]').val(); 
                $.ajax({ 
                    type:"POST", 
                    url:"includes/php/ajax.php", 
                    data:"action=rename&new_doc_name="+new_doc_name+"&doc_id="+doc_id,
                    success: function(xhr){  
                       // $('div.deleteMessage').hide(function(){$('div.deleteSuccess').fadeIn();}); 
//                            if (xhr == 'Too many characters. We limit names to 255 characters.') 
                            if ((xhr == 'Document name too long, we limit to 255 characters.') || 
                                 (xhr == 'Please enter a project name.'))
                            { 
                                $("#renameErrorDiv").html(xhr); 
                            } 
                            else 
                            {
                                $("#renameErrorDiv").html("");
                                $('input[name="new_doc_name"]').val(""); 
			        $("#dialogRenameProject").dialog("close"); 
			        $("#documents").load("includes/php/documents.php", function(response, status, xhr) {
				    if (status == "error") {
					var msg = "Sorry but there was an error: ";
					$("#documents").html(msg + xhr.status + " " + xhr.statusText);
				    }
				    else {
					$( ".deleteButtonClass").click(function() {
						// $('#deleteProject').val($('#deleteButton').attr("doc_id")); 
						//$('input[name="hidden_delete_id"]').val($(this).attr("doc_id")); 
						victimDocId = ($(this).attr("doc_id")); 
						$( "#dialogDeleteProject" ).dialog('open'); return false; 
						});
					$( ".renameButtonClass").click(function() {
						// $('#deleteProject').val($('#deleteButton').attr("doc_id")); 
						victimDocId = ($(this).attr("doc_id")); 
						$( "#dialogRenameProject" ).dialog('open'); return false; 
						});
                                        jQuery("span.timeago").timeago($("span.timeago").attr("title"));
				    }
		    	       });
                            }
                        } 
                 });
            }, 
            "Cancel" : function() { 
                $(this).dialog("close"); 
            }
    }); 
     
    $("#dialogDeleteProject").dialog('option', 'buttons', {  
            "Delete" : function() { 
                var doc_id = victimDocId;
                $.ajax({ 
                    type:"POST", 
                    url:"includes/php/ajax.php", 
                    data:"action=delete&doc_id="+doc_id,
                    success: function(){  
                       // $('div.deleteMessage').hide(function(){$('div.deleteSuccess').fadeIn();}); 
		    $("#dialogDeleteProject").dialog("close"); 
		    $("#documents").load("includes/php/documents.php", function(response, status, xhr) {
			    if (status == "error") {
			    var msg = "Sorry but there was an error: ";
			    $("#documents").html(msg + xhr.status + " " + xhr.statusText);
			    }
			    $( ".deleteButtonClass").click(function() {
				    // $('#deleteProject').val($('#deleteButton').attr("doc_id")); 
				    //$('input[name="hidden_delete_id"]').val($(this).attr("doc_id")); 
				    victimDocId = ($(this).attr("doc_id")); 
				    $( "#dialogDeleteProject" ).dialog('open'); return false; 
				    });
			    $( ".renameButtonClass").click(function() {
				    // $('#deleteProject').val($('#deleteButton').attr("doc_id")); 
				    victimDocId = ($(this).attr("doc_id")); 
				    $( "#dialogRenameProject" ).dialog('open'); return false; 
				    });
                                    jQuery("span.timeago").timeago($("span.timeago").attr("title"));

			    });
                    } 
                 });
            }, 
            "Cancel" : function() { 
                $(this).dialog("close"); 
            }
    }); 
    
/*    $( ".cancelDialogButton").click(function() {
         $("#dialogRenameProject").dialog("close"); 
    });*/
 
  });
